
import { defineComponent, h, toRaw } from 'vue';
import { format, parse } from 'date-fns';
import CalendarConverter from 'js-calendar-converter';
import { LSRUtils, } from '../../../../utils/LSRUtils';
import LocalCache from '../../../../service/local.cache';
import { Md5 } from 'ts-md5';  

import { DiZhi,  DizhIKeys, WuXingKeys, FiveStarsKeys, SixGodKeys } from '../../../../model/LSRModel';
import { UtilsCore } from '../../../../utils/UtilsCore';
import {  useRouter } from 'vue-router';
import { Toast } from 'tdesign-mobile-vue';
import { ErrorCircleIcon } from 'tdesign-icons-vue-next';

export default defineComponent({
  name: "LsrView",
  props: {

    // 如果有父组件传递的属性，在这里定义
  },
  setup() {
    console.log("setup");


    // 这里可以保留 setup 函数中定义的逻辑
    // 但需要转换为 Options API 格式 

    // const route = useRoute();
    //    //query  route.query
    //    console.log(route.query);

    //     //params route.params
    //     console.log(route.query);


    // state 传参接收 
    // console.log(history.state.params);
    let params = history.state.params;
    console.log(params);
    const router = useRouter(); 
  return  {
      WuXingKeys: WuXingKeys,
      SixGodKeys:SixGodKeys,
      FiveStarsKeys:FiveStarsKeys,
      router: router, 
    };
  },
  data() {

    let params = history.state.params;
    let sixKeysEle = LSRUtils.getLiuRen();
 
    sixKeysEle.forEach((item) => {
      item.One = false;
      item.Two = false;
      item.OneName = "";
      item.TwoName = "";
    }); 
    console.log(params); 
    if(params.id)
    {
       //这里去服务端请求
       return {};
    }
    else{
   // 本地

   let key =  params.key; 
   //第一次落宫
   let one = params.one;
   //第二次落宫
   let two = params.two;
   //起课方式
   let divination = params.divination;

   let textValue = params.textValue;
   let numValue = params.numValue;
   let annotation =  params.note || null;
   let dateValue = '', pickerValue = '', divinationValue = '';
   if (UtilsCore.isNotEmpty(params.dateValue)) {
     let date = parse(params.dateValue, "yyyy-MM-dd", new Date());
     const year = date.getFullYear();
     const month = date.getMonth() + 1;
     const curDate = date.getDate();
     const lunar = CalendarConverter.solar2lunar(year, month, curDate);
     let glDate = format(date, 'yyyy-MM-dd');
     dateValue = glDate + "(" + lunar.IMonthCn + lunar.IDayCn + ")";
   }
   if (UtilsCore.isNotEmpty(params.pickerValue)) {
     let picker = params.pickerValue;
     if (picker.split(':').length == 2) {
       let currentBranchIndex = LSRUtils.getShichen(parseInt(picker.split(':')[0]));
       let shichen = DizhIKeys[currentBranchIndex] + "时"
       let moment = LSRUtils.getShike(parseInt(picker.split(':')[0]), parseInt(picker.split(':')[1]));
       let shike = DizhIKeys[moment - 1] + "刻";
       pickerValue = picker.split(':')[0] + "点" + picker.split(':')[1] + "分 (" + shichen + shike + ")";
     }
   }

   //落宫地支
   let shichen = params.shichen;//时辰落宫  
   let isDivination = false;
   if (UtilsCore.isNotEmpty(divination) && UtilsCore.isNotEmpty(one) && UtilsCore.isNotEmpty(two) && UtilsCore.isNotEmpty(shichen)) {
     console.log("one=>"+one+"two=>"+two); 
     divinationValue = divination == 1 ? '日期起课' : divination == 2 ? '时辰起课' : '数字起课';
     isDivination = true;
     let dizhi: DiZhi = LSRUtils.getDizhi(shichen);
     sixKeysEle.forEach((item, index) => {
       //第一次落宫y
       if (item.index == one) {
         item.One = true;
         item['OneName']=divination==1?'日':divination==2?"时":"日"; 
         if (divination !== 2) {
           LSRUtils.UpFiveStars(sixKeysEle, LSRUtils.getFiveStarsWuXing(), item.index, 0);
         }
       }
       //第二次落宫
       if (item.index == two) {
         item.Two = true;
         item['TwoName'] = divination == 2 ? "刻" : "时";
         item.dizhi = dizhi; 
         //地支
         LSRUtils.GroundExhaustBranch(sixKeysEle, LSRUtils.DiZhiList(), item.index, shichen);
         //六神
         LSRUtils.PleaseSixGods(sixKeysEle, LSRUtils.getSixGodWuXing(), item.dizhi.sixRen, 0);
         //六亲
         LSRUtils.SixRelationsLocation(sixKeysEle, LSRUtils.getMutualRestraint(), index);
         if (divination === 2) {
           //  五星
           LSRUtils.UpFiveStars(sixKeysEle, LSRUtils.getFiveStarsWuXing(), item.index, 0);
         }
       }
     }); 
   }
   return {
    isDivination: isDivination, divination: divination, divinationValue: divinationValue, sixKeysEle: sixKeysEle, textValue: textValue,
    numValue: numValue, dateValue: dateValue, pickerValue: pickerValue,
    params:params,annotation:annotation,  key:key
    
  };
    }
  
   


    
  },
  methods: {
    handleClick() {
      this.router.back();
    },
    onSubmit()
    {

    },
    saveData()
    {
      try { 
   
          let   params=  toRaw(this.params);
           params['note'] =this.annotation;
  
          let hashKey =params.key;
           if(!hashKey)
           {
              hashKey =  Md5.hashStr(JSON.stringify(this.params));
              this.params['key']=hashKey;
           } 
        let qkMap:any={};
        var obj =  LocalCache.getCache("qkList"); 
        if(obj!=null)
        {
          qkMap =obj;
        } 
        qkMap[hashKey]=this.params; 
        LocalCache.setCath("qkList",qkMap); 
        Toast({
          className: 'toast-root--success',
          duration: 5000,
          theme: 'success',
          direction: 'column',
          message: '添加成功!',
        }); 
      } catch (error) {
        Toast({
          theme: 'error',
          direction: 'column',
          message: '保存错误!'+error,
        });
      }
      
      
    
     
    }

  },
  computed: {

    // 如果需要，将 setup 中的 computed 属性转换到此处

  },
  mounted() {
    console.log("mounted");
    // 如果需要，将 setup 中的 onMounted 逻辑转换到此处
  }
});